package com.un.ebs.production.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.core.enums.BooleanEnum;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.util.ReportUtils;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.production.domain.*;
import com.un.ebs.production.dto.*;
import com.un.ebs.production.service.ProductionOrderService;
import com.un.ebs.production.service.ProductionWorkOrderService;
import com.un.ebs.purchase.dto.PurchaseItemCreateDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2020-05-25
 */
@RestController
@RequestMapping("/productionOrder")
@Api(value = "production-order", tags = "")
public class ProductionOrderController {

    @Resource
    private ProductionOrderService service;

    @Resource
    private ProductionWorkOrderService productionWorkOrderService;

    @PostMapping("add")
    @ApiOperation(value = "添加")
    public WebResult save(@RequestBody @Validated ProductionOrderAddDto req) {
        return WebUtils.success(service.add(req));
    }

    @PostMapping("addBySale")
    @ApiOperation(value = "添加")
    public WebResult add(@RequestBody @Validated ProductionOrderAddBySaleDto req) {
        return WebUtils.success(service.add(req));
    }

    @PostMapping("rebuild")
    @ApiOperation(value = "重造")
    public WebResult rebuild(@RequestBody @Validated ProductionOrderRebuildDto req) {
        return WebUtils.success(service.addReBuild(req));
    }

    @PostMapping("update")
    @ApiOperation(value = "修改")
    public WebResult update(@RequestBody @Validated ProductionOrderUpdateDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @PostMapping("submit")
    @ApiOperation(value = "提交")
    public WebResult submit(@RequestParam String id) {
        service.submit(id);
        return WebUtils.success();
    }

    @PostMapping("submitCancel")
    @ApiOperation(value = "提交撤销")
    public WebResult submitCancel(@RequestParam String id) {
        service.submitCancel(id);
        return WebUtils.success();
    }

    @PostMapping("run")
    @ApiOperation(value = "执行生产订单")
    public WebResult run(@RequestParam String id, @RequestParam(required = false) String seqId) {
        service.run(id, seqId);
        return WebUtils.success();
    }

    @PostMapping("runFix")
    @ApiOperation(value = "修复", notes = "开工了，但没有生成工序的。")
    public WebResult runFix(@RequestParam String code) {
        service.runFix(code);
        return WebUtils.success();
    }

    @PostMapping("runServicePurchaseFix")
    @ApiOperation(value = "修复", notes = "开工了，但是没有生成委外服务采购订单的。")
    public WebResult runServicePurchaseFix(@RequestParam String code) {
        return WebUtils.success(service.runServicePurchaseFix(code));
    }

    @PostMapping("runServiceNotAcceptFix")
    @ApiOperation(value = "修复", notes = "完工了，但没有收货和领料的。")
    public WebResult runServiceNotAcceptFix(@RequestParam String code) {
        return WebUtils.success(service.runServicesNotAcceptFix(code));
    }

    @PostMapping("runCancel")
    @ApiOperation(value = "撤销执行生产订单")
    public WebResult runCancel(@RequestParam String id) {
        service.runCancel(id);
        return WebUtils.success();
    }

    @PostMapping("productionToPurchase")
    @ApiOperation(value = "生产转采购")
    public WebResult productionToPurchase(@RequestBody List<PurchaseItemCreateDto> req) {
        service.productionToPurchaseOrder(req);
        return WebUtils.success();
    }

    @PostMapping("productionToInventoryOut")
    @ApiOperation(value = "生产领用")
    public WebResult productionToInventoryOut(@RequestBody ProductionOrderToBomUserDto req) {
        service.productionToBomUser(req);
        return WebUtils.success();
    }

    @PostMapping("productionToInventoryOutAll")
    @ApiOperation(value = "生产领用")
    public WebResult productionToInventoryOutAll(@RequestParam String productionOrderId, @RequestParam String warehouseId) {
        service.productionToBomUser(productionOrderId, warehouseId);
        return WebUtils.success();
    }

    @PostMapping("productionSeqSkuToInventoryOut")
    @ApiOperation(value = "工序领料")
    public WebResult productionSeqSkuToInventoryOut(@RequestParam String productionOrderId, @RequestParam String warehouseId, @RequestParam String skuId, @RequestParam Double qty) {
        service.productionToBomUser(productionOrderId, warehouseId, skuId, qty);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "详情", notes = "根据主键获取单个 ProductionOrder")
    public WebResult<ProductionOrderVo> getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryViewById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "查询", notes = "")
    public WebResult<IPage<ProductionOrderExt>> list(@RequestParam(required = false) String searchText, @RequestParam(required = false) BooleanEnum runStatus, @RequestParam(required = false) BooleanEnum isComplete, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.query(StringUtils.isNullOrEmpty(searchText) ? null : searchText.trim(), runStatus, isComplete, pageSize, pageIndex));
    }

    @GetMapping("listBySaleNoteId")
    @ApiOperation(value = "查询，按销售单号", notes = "")
    public WebResult<IPage<ProductionOrderExt>> list(@RequestParam String id, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryBySaleOrderId(id, pageSize, pageIndex));
    }

    @GetMapping("listItem")
    @ApiOperation(value = "明细", notes = "")
    public WebResult<IPage<ProductionOrderItemExt>> listItem(@RequestParam String id, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryItemById(id, pageSize, pageIndex));
    }

    @GetMapping("listItemAll")
    @ApiOperation(value = "明细", notes = "")
    public WebResult<IPage<ProductionOrderItemExt>> listItem(@RequestParam String searchText, @RequestParam(required = false) BooleanEnum runStatus, @RequestParam(required = false) BooleanEnum isComplete, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryItem(StringUtils.isNullOrEmpty(searchText) ? null : searchText.trim(), runStatus, isComplete, pageSize, pageIndex));
    }

    @GetMapping("listItemBom")
    @ApiOperation(value = "明细BOM", notes = "")
    public WebResult<IPage<ProductionOrderItemBomExt>> listItemBom(@RequestParam String productionOrderId, @RequestParam BooleanEnum lackStatus, @RequestParam String skuId, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryItemBom(productionOrderId, lackStatus, skuId, pageSize, pageIndex));
    }

    @GetMapping("listItemBomSum")
    @ApiOperation(value = "明细BOM，按订单汇总", notes = "")
    public WebResult<IPage<ProductionOrderItemBomSumDto>> listItemBomSum(@RequestParam(required = false) String searchText, @RequestParam String productionOrderId, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryItemBomSum(StringUtils.isNullOrEmpty(searchText) ? null : searchText.trim(), productionOrderId, pageSize, pageIndex));
    }

    @GetMapping("listRequirementItem")
    @ApiOperation(value = "查询生产物料需求明细")
    public WebResult<IPage<ProductionOrderRequirementPlanExt>> listRequirementItem(@RequestParam(required = false) String searchText, @RequestParam(required = false) String skuId, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryRequirementItem(StringUtils.isNullOrEmpty(searchText) ? null : searchText.trim(), skuId, pageSize, pageIndex));
    }

    @GetMapping("listRequirement")
    @ApiOperation(value = "查询生产物料需求")
    public WebResult<IPage<ProductionRequirementPlanExt>> listRequirement(@RequestParam(required = false) String searchText, @RequestParam(required = false) String supplierEntId, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryRequirement(StringUtils.isNullOrEmpty(searchText) ? null : searchText.trim(), supplierEntId, pageSize, pageIndex));
    }

    @GetMapping("listView")
    @ApiOperation(value = "查询")
    public WebResult<IPage<ProductionOrderView>> queryView(@RequestParam String searchText, @RequestParam(required = false) BooleanEnum runStatus, @RequestParam(required = false) BooleanEnum isComplete, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryView(StringUtils.isNullOrEmpty(searchText) ? null : searchText.trim(), runStatus, isComplete, pageSize, pageIndex));
    }

    @GetMapping("listCostDiff")
    @ApiOperation(value = "查询，成本差异")
    public WebResult<IPage<ProductionCostDiffExt>> queryCostDiff(@RequestParam String startMonth, @RequestParam String endMonth, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryCostMoneyDiff(startMonth, endMonth, pageSize, pageIndex));
    }

    @GetMapping("listViewByCanInInventory")
    @ApiOperation(value = "具备生产入库的生产订单")
    public WebResult<IPage<ProductionOrderView>> queryViewByCanInInventory(@RequestParam String searchText, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(service.queryViewByCanInInventory(StringUtils.isNullOrEmpty(searchText) ? null : searchText.trim(), pageSize, pageIndex));
    }

    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除", notes = "根据主键删除单个 ProductionOrder")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @GetMapping("printBomUser")
    @ApiOperation("打印，领料单")
    public void printBomUser(@RequestParam String orderId, HttpServletResponse response) {
        List<ProductionOrderItemBomSumExt> list = service.queryItemBomSum(null, orderId, -1, 1).getRecords();
        ProductionOrderVo obj = service.queryViewById(orderId);
        if (obj.getItems().size() != 1) {
            throw new BusinessException("生产订单明细数量不为1");
        }
        ReportUtils.print(obj.getItems().stream().findFirst().get(), list, "reports/prod_ord_bom_user.jrxml", response);
    }

    @GetMapping("printWorkOrder")
    @ApiOperation("打印，派工单")
    public void print(@RequestParam String itemId, HttpServletResponse response) {
        List<ProductionWorkOrderExt> list = productionWorkOrderService.query(itemId);
//        if(list.size()<=0){
//            throw new BusinessException("没有工序数据，请检查产品是否指定了工序集。");
//        }
        ReportUtils.print(service.queryItemByItemId(itemId), list, "reports/work_order.jrxml", response);
    }

    @GetMapping("printWorkOrderOut")
    @ApiOperation("打印，工序派工单")
    public void print(@RequestParam String itemId, @RequestParam String skuId, HttpServletResponse response) {
        List<ProductionWorkOrderOutExt> list = productionWorkOrderService.queryOut(itemId, skuId);
        ReportUtils.print(null, list, "reports/work_order_out.jrxml", response);
    }

}